
function Main
{
    Param(
        [parameter(Mandatory=$true)]
        [string[]]$iis_webSite
    )

    Process
    {
        try{
            Get-WriteLog('')
            Get-WriteLogFormat('Visit Project Start')
            Get-WriteLog('')

            Get-WriteLogFormat('Setting Info Start')
            Get-WriteLog('visit Web Site: ' + $iis_webSite)
            Get-WriteLogFormat('Setting Info Stop')
            Get-WriteLog('')

            Get-WriteLogFormat('Start App Pool - Running')
            # 5.iis 程序池啟動
            foreach($site in $iis_webSite)
            {
                $site_info = Get-WebSite -Name $site
                if($site_info -ne $null)
                {
                    $pool = $site_info.applicationPool
                    if(($pool -ne $null) -and ($pool -ne ''))
                    {
                        $status = Get-WebAppPoolState -Name $pool
                        Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        if($status.Value -eq "Stopped")
                        {
                            Get-WriteLog('Start App Pool: ' + $pool + ' Start')
                            Start-WebAppPool -Name $pool
                            Get-WriteLog('Start App Pool: ' + $pool + ' End')

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                        }elseif($status.Value -eq "Stopping")
                        {
                            # Stopping 等待轉成 Stopped 
                            do{
                                # 休眠1秒
                                Start-Sleep -Seconds 1
                                Get-WriteLog('Sleep: 1')
                            } while($status.Value -eq "Stopping")

                            $status = Get-WebAppPoolState -Name $pool
                            Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)

                            if($status.Value -eq "Stopped")
                            {
                                Get-WriteLog('Start App Pool: ' + $pool + ' Start')
                                Start-WebAppPool -Name $pool
                                Get-WriteLog('Start App Pool: ' + $pool + ' End')

                                $status = Get-WebAppPoolState -Name $pool
                                Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                            }
                        }
                        Get-WriteLog('-------------')
                    }
                }
            }
            Get-WriteLogFormat('Start App Pool - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Start Web Site - Running')
            # 6.iis 網站啟動
            foreach($site in $iis_webSite)
            {
                $status = Get-WebSiteState -Name $site
                Get-WriteLog('Web Site: ' + $site + ',State: ' + $status.Value)
                if($status.Value -eq "Stopped")
                {
                    Get-WriteLog('Start Web Site: ' + $site + ' Start')
                    Start-WebSite -Name $site
                    Get-WriteLog('Start Web Site: ' + $site + ' End')

                    $status = Get-WebAppPoolState -Name $pool
                    Get-WriteLog('App Pool: ' + $pool + ',State: ' + $status.Value)
                }
                Get-WriteLog('-------------')
            }
            Get-WriteLogFormat('Start Web Site - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Visit Web Site - Running')
           
            #iis 訪問網站
            foreach($site in $iis_webSite)
            {
                [string[]] $urls = Get-IISWebSiteUrl -name $site
                Get-WriteLog('all url: ')
                Get-WriteLogArr($urls)
                Get-WriteLog('-------------')
                if($urls.count -gt 0)
                {
                    Get-WriteLog('visit url: ' + $urls[0])    
                    #訪問綁定的第一個網址   
                    $ie = Invoke-WebRequest $urls[0] 
                }
                Get-WriteLog('-------------')
            }
            Get-WriteLogFormat('Visit Web Site - End')
            Get-WriteLog('')

            Get-WriteLogFormat('Visit Project End')
            Get-WriteLog('')
        }
        catch{
            $errorInfo = "error:" + $Error[0]
            $errorInfo
            $Global:logInfo = $Global:logInfo + $errorInfo 
        }
        Finally{
            $Error.clear()
        }
    }
}

